package com.wf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wf.dto.InvoiceDTO;
import com.wf.service.InvoiceService;
import com.wf.vo.Invoice;
import nuonuo.open.sdk.NNOpenSDK;
import org.springframework.stereotype.Service;


/**
 * @Author: zhiyu
 * @Date:14:13
 * @DESC:
 */
@Service
public class InvoiceServiceImpl implements InvoiceService {
    @Override
    public String submitInvoice(InvoiceDTO invoiceDTO,String token,String senid) {
        NNOpenSDK sdk = NNOpenSDK.getIntance();
        String method = "nuonuo.ElectronInvoice.requestBillingNew";//API方法名
        String orderNo = String.valueOf(System.currentTimeMillis());
        String content = "{\"order\":"
                // 发票明细
                + "{\"invoiceDetail\":[" + "{"
                // 税率
                + "\"taxRate\":\"" + 0.13 + "\","
                // 发票行性质:0,正常行;1,折扣行;2,被折扣行
                + "\"invoiceLineProperty\":\"" + 0 + "\","
                // 商品名
                + "\"goodsName\":\"" + "租赁费用" + "\","
                + "\"goodsCode\":\"" + "3040801" + "\","
                // 不含税金额
                + "\"taxExcludedAmount\":\"" + invoiceDTO.getTotalAmount() + "\","
                // 单价含税标志 0 不含税 1 含税
                + "\"withTaxFlag\":\"" + 0 + "\","
                // 数量
                + "\"num\":\"" + 1 + "\","
                // 税额 [不含税金额] * [税率] = [税额]
                + "\"tax\":\"" + 0.13*(Double.parseDouble(invoiceDTO.getTotalAmount())) + "\","
                // 单位
                + "\"unit\":\"" + "" + "\","
                // 扣除额，差额征税时填写
                + "\"deduction\":\"" + 0 + "\","
                // 单价（精确到小数点后8位）
                + "\"price\":\"" + invoiceDTO.getTotalAmount() + "\"}],"
                // 购方名称 Y
                + "\"buyerName\":\"" + invoiceDTO.getEnterName() + "\","
                // 购方地址 N
                + "\"buyerAddress\":\"" + "测试地址" + "\","
                // 购方税号（企业要填，个人可为空） N
                + "\"buyerTaxNum\":\"" + invoiceDTO.getEnterNumber()+ "\","
                // 开票类型：1:蓝票;2:红票
                + "\"invoiceType\":\"" + 1 + "\","
                // 发票种类
                + "\"invoiceLine\":\"" + "p" + "\","
                // 清单标志：非清单:0；清单:1，默认:0，电票固定为0
                + "\"listFlag\":\"" + 0 + "\","
                // 推送方式：-1,不推送;0,邮箱;1,手机（默认）;2,邮箱、手机
                + "\"pushMode\":\"" + 2 + "\","
                // 购方手机（开票成功会短信提醒购方，不受推送方式影响） Y
                + "\"buyerPhone\":\"" + "15343706015" + "\","
                // 邮箱地址，沙箱环境也可以发送
                + "\"email\":\"" + invoiceDTO.getEmail() + "\","
                // 开票员id（诺诺系统中的id） N
                + "\"clerkId\":\"" + "ceshi" + "\","
                + "\"payer\":\"" + "nickname" + "\","
                // 销方银行账号和开户行地址
                + "\"salerAccountr\":\"" + "测试银行测试支行120200590990432278" + "\","
                + "\"salerAddressr\":\"" + "测试银行地址" + "\","
                // 订单号（每个企业唯一） Y
                + "\"orderNo\":\"" + invoiceDTO.getTrade_no() + "\","
                // 销方电话
                + "\"salerTelr\":\"" + "0731-168168168" + "\","
                // 订单时间 Y
                + "\"invoiceDate\":\"" + "2024-09-22 12:30:00" + "\","
                // 开票员 Y
                + "\"clerk\":\"" + "测试" + "\","
                // 分机号（只能为空或者数字）
                + "\"extensionNumber\":\"" + "0" + "\","
                // 销方税号
                + "\"salerTaxNum\":\"" + "339901999999142" + "\","
                + "\"invoiceCode\": \"123456789012\"}}";
        return sdk.sendPostSyncRequest(Invoice.url,senid,Invoice.appKey,Invoice.APPSecret,token,Invoice.taxnum,method,content);
    }

    @Override
    public String getResult(String invoiceCode,String token,String senid) {
        System.out.println("invoiceCode = " + invoiceCode);
        JSONObject parsed = JSONObject.parseObject(invoiceCode);
        System.out.println("parsed = " + parsed);
        JSONObject parse = JSONObject.parseObject(parsed.get("result")+"");
        Object object = parse.get("invoiceSerialNum");
        NNOpenSDK sdk = NNOpenSDK.getIntance();
        String method = "nuonuo.ElectronInvoice.queryInvoiceResult"; // API方法名
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("serialNos",object);
        jsonObject.put("isOfferInvoiceDetail","0");
        String content = jsonObject.toJSONString();
        String s = sdk.sendPostSyncRequest(Invoice.url, senid, Invoice.appKey, Invoice.APPSecret, token, Invoice.taxnum, method, content);
        return s;
    }
}
